package com.feiyi.app.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.feiyi.app.R;
import com.feiyi.app.adapter.JifenAdapter;
import com.feiyi.app.adapter.MyYouhuiAdapter;
import com.feiyi.app.app.AppConstant;
import com.feiyi.app.base.BaseActivity;
import com.feiyi.app.bean.JifenBean;
import com.feiyi.app.bean.MyYouhuiBean;
import com.feiyi.app.event.BonusChooseEvent;
import com.feiyi.app.net.HttpMethods;
import com.feiyi.app.view.CustomLoadMoreView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.FormBody;
import okhttp3.RequestBody;

/**
 * @author wangliantao
 * @time 2018 11 2018/11/18 6:00 PM
 * @email 1346947404@qq.com
 * @package com.feiyi.app.activity
 * @describe： In me the tiger sniffs the rose
 */
public class MyYouhuiActivity extends BaseActivity implements OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener {

    Unbinder unbinder;
    @BindView(R.id.refreshlayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;
    @BindView(R.id.tv_title_titlebar)
    TextView tv_title;

    private List<MyYouhuiBean.DataBean> mList = new ArrayList<>();

    private MyYouhuiAdapter mAdapter;

    private int page = 1;

    private static final String TAG = "main";

    private String uid;
    private String sid;

    private String choosingBonus;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        unbinder = ButterKnife.bind(this);
        initView();
        initData();
    }

    private void initView() {
        uid = spUtils.getString(AppConstant.UID);
        sid = spUtils.getString(AppConstant.SID);
        choosingBonus = getIntent().getStringExtra("choosingBonus");
        tv_title.setText("我的优惠券");
        refreshLayout.setOnRefreshListener(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
        mAdapter = new MyYouhuiAdapter(R.layout.item_youhuiquan,mList);
        mAdapter.setLoadMoreView(new CustomLoadMoreView());
        mAdapter.setOnLoadMoreListener(this,recyclerView);
        mAdapter.disableLoadMoreIfNotFullPage(recyclerView);
        recyclerView.setAdapter(mAdapter);
        if (null != choosingBonus){
            recyclerView.addOnItemTouchListener(new OnItemClickListener() {
                @Override
                public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                    EventBus.getDefault().post(new BonusChooseEvent(mList.get(position)));
                    finish();
                }
            });
        }


    }

    private void initData() {
        FormBody.Builder builder = new FormBody.Builder();
        if (null != uid && !uid.isEmpty())
            builder.add("uid",uid);
        if (null != sid && !sid.isEmpty())
            builder.add("sid",sid);
        builder.add("page","1");
        builder.add("count","10");
        RequestBody body = builder.build();

        HttpMethods.getInstance().info.getYouhuiList(AppConstant.YOUHUI_URL,body).
                subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<MyYouhuiBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(MyYouhuiBean goodsSearchBean) {
                        Log.e(TAG, "onNext: 111" );
                        if (null != goodsSearchBean.getData()){
                            mList.addAll(goodsSearchBean.getData());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e(TAG, "获取优惠信息失败 "+e.toString() );
                    }

                    @Override
                    public void onComplete() {
                        mAdapter.notifyDataSetChanged();
                    }
                });

    }

    @OnClick({R.id.iv_back_titlebar})
    public void onClick(View view){
        switch (view.getId()) {
            case R.id.iv_back_titlebar:
                finish();
                break;

        }
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        page = 1;
        mList.clear();
        initData();
        refreshLayout.finishRefresh(3000);
    }

    @Override
    public void onLoadMoreRequested() {

        page++;

        FormBody.Builder builder = new FormBody.Builder();
        if (null != uid && !uid.isEmpty())
            builder.add("uid",uid);
        if (null != sid && !sid.isEmpty())
            builder.add("sid",sid);
        builder.add("page",page+"");
        builder.add("count","10");
        RequestBody body = builder.build();

        HttpMethods.getInstance().info.getYouhuiList(AppConstant.YOUHUI_URL,body).
                subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<MyYouhuiBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(MyYouhuiBean goodsSearchBean) {
                        Log.e(TAG, "onNext: 111" );
                        if (null != goodsSearchBean.getData()){
                            mList.addAll(goodsSearchBean.getData());
                        }else {
                            page--;
                            mAdapter.loadMoreEnd();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        page--;
                        mAdapter.loadMoreFail();

                        Log.e(TAG, "获取优惠信息失败 "+e.toString() );
                    }

                    @Override
                    public void onComplete() {
                        mAdapter.loadMoreEnd();
                        mAdapter.notifyDataSetChanged();
                    }
                });
    }
}
